package com.receiver.location;

import java.util.ArrayList;
import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class LocationReceiverActivity extends MapActivity {
	static final int ADD_MARK = 0;
	static final int REMOVE_MARK = 1;
	MapView mapView;
	List<Overlay> mapOverlays;
	Drawable markerImg;
    FirstOverlay firstOverlay;
    GeoPoint geoPoint;
    MapController mapController;
    int count = 0;
    long firstClick = 0;
    long secondClick = 0;
    List<FirstOverlay> overlayList;
	
	/**接受包含经纬度信息的double[]*/
	 public Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch(msg.what){
			case ADD_MARK:{
				
				markMap((double[])msg.obj, msg.arg1);
			};break;
			case REMOVE_MARK:{
				removeMark(msg.arg1);
			};break;
			}
		}
	};
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        mapView = (MapView)findViewById(R.id.map);
        mapView.setBuiltInZoomControls(true);	//设置用于缩放地图的工具条
        mapView.setSatellite(true); 	//设置为卫星地图（没有偏置）
        mapController = mapView.getController();
        mapOverlays = mapView.getOverlays();

        markerImg = getResources().getDrawable(R.drawable.red_dot);
        firstOverlay = new FirstOverlay(markerImg, this);
        
        overlayList = new ArrayList<FirstOverlay>();
        //test
        Message msg = handler.obtainMessage();
        double[] d = {113.5, 23.053};
        msg.what = ADD_MARK;
        msg.arg1 = 10086;
        msg.obj = d;
        handler.sendMessage(msg);
    }
	
	/**在地图上标记求助点*/
	private void markMap(double[] d, int phoneNum){
		geoPoint = new GeoPoint((int)(d[1]*Math.pow(10, 6)), (int)(d[0]*Math.pow(10, 6)));
		mapController.animateTo(geoPoint);
		mapController.setZoom(16);
		
		OverlayItem overlayitem = new OverlayItem(geoPoint, phoneNum + "", "");
        firstOverlay.addOverlay(overlayitem);		//添加item到图层      
        overlayList.add(firstOverlay);
        mapOverlays.add(firstOverlay);		//添加图层到地图
	}
	
	/**移除标记点*/
	private void removeMark(int phoneNum){
		for(int i = 0; i < overlayList.size(); i++){
			if(overlayList.get(i).msg.arg1 == phoneNum){
				mapOverlays.remove(overlayList.get(i));
			}
		}
	}
	
	
	/**响应双击地图事件*/
	@Override  
    public boolean dispatchTouchEvent(MotionEvent ev) {  
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {  
            count++;  
            if(count==1){  
                firstClick = System.currentTimeMillis();  
            }else if(count==2){  
                secondClick = System.currentTimeMillis();  
                if (secondClick - firstClick < 500) {  
                    mapView.getController().zoomInFixing((int) ev.getX(),  
                            (int) ev.getY());  
                }  
                count = 0;  
                firstClick = 0;  
                secondClick = 0;  
            }  
        }  
        return super.dispatchTouchEvent(ev);  
    }  

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}